package com.jbm.jbmsupplier.zp.FactouryActivity.FacturyChat;

import android.content.Context;
import android.os.Handler;

import com.jbm.jbmsupplier.net.NetinterSend;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.onehome.common.OHInterface;

import java.util.ArrayList;

import static com.jbm.jbmsupplier.zp.utils.ClassUser.getDate;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHAT;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHATGROUD;
import static com.jbm.jbmsupplier.zp.utils.TableName.RECENT;
import static com.jbm.jbmsupplier.zp.utils.TableName.sendIndex;

/**
 * Created by 家帮忙 on 2017/7/12.
 */

public class SendText implements SendAllMassage{
    private String date;
    @Override
    public ChatMsgEntity setSendMassage(String tex, int to_id,String toname, String user, Context context,ClassUser classUser,Handler handler,int tag,int cgid) {
        date=getDate();
        NetinterSend netinterSend = new NetinterSend(handler);
        if (cgid!=-1){
            netinterSend.SetSendTextMess(cgid,to_id, tag, tex);
        }else{
            netinterSend.SetSendTextMess(-1,to_id, tag, tex);
        }
        ChatMsgEntity entity = new ChatMsgEntity();
        entity.setDate(date);
        entity.setName(toname);
        entity.setTo_id(to_id);
        entity.setLocal_id(classUser.getLogin());
        entity.setText(tex);
        entity.setFlag(7);
        entity.setIsboolean("true");
        entity.setHead_portrait(OHInterface.getG_server().logo);
        sendIndex+=1;
        if (cgid!=-1){
            MySqliteUtlis.GetInstan(context).InsertGroadTable(CHATGROUD ,toname, OHInterface.getG_server().logo, date,tex,"",  to_id,-1,-1,-0,"","","","",0,"","", 7,cgid,1,"true",classUser.getLogin());
        }else {
            try {
                MySqliteUtlis.GetInstan(context).StoringChatData(CHAT, to_id, OHInterface.getG_server().logo, tex, date, toname, "", "", "", "", "", "", "", "", "", 7, 1, "true",classUser.getLogin());
                ArrayList<String> als = MySqliteUtlis.GetInstan(context).showaRecentContactsid(RECENT,to_id,classUser.getLogin());
                if (!als.toString().contains(to_id+"")) {
                    MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT , to_id,classUser.getLogin(),date);
                }else if (als.toString()==null){
                    MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT , to_id,classUser.getLogin(),date);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

}
